package 多线程;

/**
 * valatile实现线程通信，不是线程安全的
 */
public class Signal {
    private static volatile int signal = 0;

    static class ThreadA implements Runnable{

        @Override
        public void run() {
            while(signal<5){
                if(signal%2==0){
                    System.out.println("ThreadA: "+signal);
                    signal++;
                }
            }
        }
    }

    static class ThreadB implements Runnable {

        @Override
        public void run() {
            while(signal<5){
                if(signal%2==1){
                    System.out.println("ThreadB: "+signal);
                    signal++;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new ThreadA()).start();
        Thread.sleep(1000);
        new Thread(new ThreadB()).start();
    }

}
